package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.GuiZeHtmlBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import org.json.JSONException;

import okhttp3.Call;

/**
 * 作者：Android
 * 时间：2019/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class CanSaiBaoMingXunHuiDetailsDialog extends Dialog {

    private Context mContext;
    private TextView mTvTitle;
    private CheckBox mCheckBox;
    private TextView mTvSure;
    private TextView mTvContent;

    private String titleStr, typeStr;


    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器


    /**
     * 设置确定按钮的显示内容和监听
     */
    public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }

    public CanSaiBaoMingXunHuiDetailsDialog(Context context) {
        super(context, R.style.MoHUDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_cansai_baoming_details_xunhui_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCheckBox.isChecked() && yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                    dismiss();
                } else {
                    ToastUtils.showText(getContext().getString(R.string.string_check_agree_tour_rule));
                }
            }
        });

    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (BaseMethod.notNull(titleStr)) {
            mTvTitle.setText(titleStr);
        }
        guizeHtml();
    }

    //规则
    private void guizeHtml() {
        PhoneLive.guiZeHtml(typeStr, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            Gson gson = new Gson();
                            GuiZeHtmlBean json = gson.fromJson(response, GuiZeHtmlBean.class);
                            if (json != null) {
                                GuiZeHtmlBean.DataBean data = json.getData();
                                if (data != null) {
                                    String content = data.getContent();
                                    if (BaseMethod.notNull(content)) {
                                        mTvContent.setText(new HtmlSpanner().fromHtml(content));
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(mContext.getString(R.string.string_response_error));
                }
            }
        });
    }
/*    //打开本包内asset目录下的html文件
    private void readHtmlFormAssets() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        // 设置可以支持缩放，两个方法必须同时设置，设置后或出现放大加号按钮
        webSettings.setSupportZoom(true);
        // 设置出现缩放工具
        webSettings.setBuiltInZoomControls(true);
        //新方法
        webSettings.setTextZoom(460);//默认值为100也就是不放大字体
        webView.setBackgroundColor(Color.TRANSPARENT);  //  WebView 背景透明效果，
        webView.loadUrl("file:///android_asset/xunhuisaiguize.html");
        //当数据加载完成后隐藏缩放按钮
        webSettings.setDisplayZoomControls(false);
    }*/

    /**
     * 初始化界面控件
     */
    private void initView() {

        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mCheckBox = (CheckBox) findViewById(R.id.check_box);
        mTvSure = (TextView) findViewById(R.id.tv_sure);
        mTvContent = (TextView) findViewById(R.id.tv_content);

    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    public void setType(String type) {
        typeStr = type;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick();
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}
